<?php

namespace App\Console\Commands;

use App\model\UserWechat;
use Illuminate\Console\Command;

class UserWechatCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'user:wechat';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '用户描述';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $app=\EasyWeChat::officialAccount();
        $nextOpenid=true;
        while($nextOpenid){

            if ($nextOpenid===true){
                $userList=  $app->user->list();
            }else{
                $userList=$app->user->list($nextOpenid);

            }
            if(!empty($userList['data']['openid'])){
                foreach ($userList['data']['openid'] as $openid){
                    $userWechat=  UserWechat::whereOpenid($openid)->first();
                    if (!$userWechat){
                      //  $this->line($openid);
                        $user=$app->user->get($openid);
                        $userWe= new UserWechat();
                        $userWe->openid=$openid;
                        $userWe->unionid=$user['unionid'];
                        $userWe->save();
                    }
                }
                $nextOpenid=$userList['next_openid'];
            }else{
                $nextOpenid=false;
            }
        }
    }
}
